package com.example.win.android_banduyujia.lib.util;

import java.util.UUID;
import java.util.Vector;

/**
 * Created by huchun on 2015/8/9.
 */
public class StrUtils {

    public static char[] hexChars = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

    public static boolean isEmpty(String str) {
        return (str == null)||(str.length() == 0);
    }

    public static String notNullStr(String value) {
        return (value == null) ? "" : value;
    }

    public static int toInt(String str, int dft) {
       if(isEmpty(str)){
           return dft;
       }

        int ret = dft;
        try{
            ret = Integer.parseInt(str);
        }catch (NumberFormatException ex){
            ret = dft;
        }
        return ret;
    }

    public static long toLong(String str, long dft) {
        if(isEmpty(str)){
            return dft;
        }

        long ret = dft;
        try{
            ret = Long.parseLong(str);
        }catch (NumberFormatException ex){
            ret = dft;
        }
        return ret;
    }

    public static String[] split(String source, char sep) {
        if(isEmpty(source)){
            return new String[]{" "};
        }

        int len = source.length();
        Vector<String>  list = new Vector<String>();
        int  i = 0;
        int  start=0;

        while(i < len){
            if(source.charAt(i) == sep){
                list.addElement(source.substring(start, i));
                start = i + 1;
            }
            i ++;
        }
        if(i > start){
            list.addElement(source.substring(start));
        }else if(source.charAt(len -1)==sep){
            list.addElement("");
        }

        String[]  arr = new String[list.size()];
        list.copyInto(arr);
        return arr;
    }

    /**
     * 创建UUID
     */
    public static String createUUID(){
        return UUID.randomUUID().toString();
    }


}
